package com.sise.rsc.repository;

import com.sise.rsc.entity.APackage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface APackageRepository extends JpaRepository<APackage,Integer> {

    APackage findAPackageById(int id);
    APackage findAPackageByNo(String no);
    List<APackage> findAPackagesByState(int state);
    //查询所有包裹信息
    @Query("from APackage")
    List<APackage> findAllAPackage();
    //查询某一个快递员今天别分配的所有包裹信息
    @Query("from APackage a right join Task t on a.id=t.aPackage.id where t.courier.id=:courierId")
    List<APackage> findAllAPackageByCourierId(int courierId);

    //查询某一个快递员今天别分配的所有收取包裹信息
    @Query("from APackage a right join Task t on a.id=t.aPackage.id where t.courier.id=:courierId and t.taskName='取件'")
    List<APackage> findAllFetchAPackageByCourierId(int courierId);
    //查询某一个快递员今天别分配的所有派送包裹信息
    @Query("from APackage a right join Task t on a.id=t.aPackage.id where t.courier.id=:courierId and t.taskName='派件'")
    List<APackage> findAllDeliverAPackageByCourierId(int courierId);

    @Modifying
    @Transactional
    @Query("update APackage a set a.state=?1 where a.id=?2")
    void updateAPackageState( int state,  int id);



    @Query("from APackage a where a.user.id=?1 order by a.createTime desc")
    List<APackage> findAllAPackageByUserId(int uid);

    @Modifying
    @Query("update APackage a set a.state=?1 where a.id=?2")
    void updateAPackageStateById(int state, int id);

    @Modifying
    @Query("update APackage a set a.state=?1 where a.no=?2")
    void updateAPackageStateByNo(int state,String no);

}
